package com.ruoyi.community.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.CpcMemberInfo;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CpcMemberMapper;
import com.ruoyi.community.domain.CpcMember;
import com.ruoyi.community.service.ICpcMemberService;

/**
 * 党员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-15
 */
@Service
public class CpcMemberServiceImpl implements ICpcMemberService 
{
    @Autowired
    private CpcMemberMapper cpcMemberMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询党员
     * 
     * @param id 党员ID
     * @return 党员
     */
    @Override
    public CpcMember selectCpcMemberById(String id)
    {
        return cpcMemberMapper.selectCpcMemberById(id);
    }

    /**
     * 查询党员列表
     * 
     * @param cpcMember 党员
     * @return 党员
     */
    @Override
    public List<CpcMember> selectCpcMemberList(CpcMember cpcMember)
    {
        return cpcMemberMapper.selectCpcMemberList(cpcMember);
    }

    /**
     * 查询党员及用户信息列表
     *
     * @param cpcMember 党员
     * @return 党员集合
     */
    @Override
    public List<CpcMemberInfo> selectCpcMemberInfoList(CpcMember cpcMember)
    {
        cpcMember.setCommunityId(SecurityUtils.getCommunityId());
        List<CpcMemberInfo> list = new ArrayList<>();
        List<CpcMember> baseList = cpcMemberMapper.selectCpcMemberList(cpcMember);
        for (int i = 0; i < baseList.size(); i++) {
            SysUser user = sysUserMapper.selectUserOnlyById(baseList.get(i).getUserId().toString());
            CpcMemberInfo cpcMemberInfo = new CpcMemberInfo();
            cpcMemberInfo.InitFromCpcMember(baseList.get(i));
            cpcMemberInfo.setUserName(user.getUserName());
            cpcMemberInfo.setNickName(user.getNickName());
            list.add(cpcMemberInfo);
        }
        return list;
    }

    /**
     * 新增党员
     * 
     * @param sysUser 系统用户
     * @return 结果
     */
    @Override
    public int insertCpcMember(SysUser sysUser)
    {
        CpcMember cpcMember = new CpcMember();
        cpcMember.setUserId(sysUser.getUserId());
        cpcMember.setGender(sysUser.getSex());
        cpcMember.setUserPhone(sysUser.getPhonenumber());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        cpcMember.setCommunityId(loginUser.getCommunityId());
        cpcMember.setCreater(loginUser.getUsername());
        Date date = DateUtils.getNowDate();
        cpcMember.setSyncTime(date);
        return cpcMemberMapper.insertCpcMember(cpcMember);
    }

    /**
     * 修改党员
     * 
     * @param cpcMember 党员
     * @return 结果
     */
    @Override
    public int updateCpcMember(CpcMember cpcMember)
    {
        return cpcMemberMapper.updateCpcMember(cpcMember);
    }

    /**
     * 批量删除党员
     * 
     * @param ids 需要删除的党员ID
     * @return 结果
     */
    @Override
    public int deleteCpcMemberByIds(String[] ids)
    {
        return cpcMemberMapper.deleteCpcMemberByIds(ids);
    }

    /**
     * 删除党员信息
     * 
     * @param id 党员ID
     * @return 结果
     */
    @Override
    public int deleteCpcMemberById(String id)
    {
        return cpcMemberMapper.deleteCpcMemberById(id);
    }
}
